package com.zmn.uac.api.controllers.oauth.login;

import com.zmn.common.utils.http.HttpUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.uac.api.constant.HeaderParamKeyConst;
import com.zmn.uac.api.controllers.BaseController;
import com.zmn.uac.common.dio.oauth.login.UserAuthLoginDIO;
import com.zmn.uac.common.enums.ShopTypeEnum;
import com.zmn.uac.common.exception.ParamsException;
import com.zmn.uac.model.bo.http.HeaderBO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.CharEncoding;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.util.Optional;
import java.util.UUID;

/**
 * 类描述：登录基础控制器
 *
 * @author xujie
 * @since 2021/05/18 17:15
 */
@Slf4j
public class LoginBaseController extends BaseController {

    private static final String TAG = "登录基础控制器";

    /**
     * 获取所有请求头信息
     *
     * @param request request
     * @return 请求头信息
     * @author xujie
     * @since 2021/05/18
     */
    protected HeaderBO getAllHeader(HttpServletRequest request) {
        try {
            Integer shopId = Integer.valueOf(Optional.ofNullable(request.getHeader(HeaderParamKeyConst.SHOP_ID_KEY)).orElse("0"));
            Integer platId = Integer.valueOf(Optional.ofNullable(request.getHeader(HeaderParamKeyConst.PLAT_ID_KEY)).orElse("0"));
            Integer channelId = Integer.valueOf(Optional.ofNullable(request.getHeader(HeaderParamKeyConst.CHANNEL_ID_KEY)).orElse("0"));
            Integer spId = Integer.valueOf(Optional.ofNullable(request.getHeader(HeaderParamKeyConst.SPID_ID_KEY)).orElse("0"));
            Integer provinceId = Integer.valueOf(Optional.ofNullable(request.getHeader(HeaderParamKeyConst.PROVINCE_ID_KEY)).orElse("0"));
            Integer cityId = Integer.valueOf(Optional.ofNullable(request.getHeader(HeaderParamKeyConst.CITY_ID_KEY)).orElse("0"));
            String provinceName = Optional.ofNullable(request.getHeader(HeaderParamKeyConst.PROVINCE_NAME_KEY)).orElse("");
            String cityName = Optional.ofNullable(request.getHeader(HeaderParamKeyConst.CITY_NAME_KEY)).orElse("");
            Integer appType = Integer.valueOf(Optional.ofNullable(request.getHeader(HeaderParamKeyConst.APP_TYPE)).orElse("0"));
            String orderChannelIdStr = Optional.ofNullable(request.getHeader(HeaderParamKeyConst.ORDER_CHANNEL_KEY)).orElse("");
            String appVersion = Optional.ofNullable(request.getHeader(HeaderParamKeyConst.APP_VERSION)).orElse("");
            Integer orderChannelId = StringUtils.isBlank(orderChannelIdStr) ? null : Integer.valueOf(orderChannelIdStr);
            Integer accessType = Integer.valueOf(Optional.ofNullable(request.getHeader(HeaderParamKeyConst.ACCESS_TYPE)).orElse("1"));

            provinceName = URLDecoder.decode(provinceName, CharEncoding.UTF_8);
            cityName = URLDecoder.decode(cityName, CharEncoding.UTF_8);
            return HeaderBO.builder()
                    .platId(platId)
                    .channelId(channelId)
                    .orderChannelId(orderChannelId)
                    .shopId(shopId)
                    .spId(spId)
                    .provinceId(provinceId)
                    .cityId(cityId)
                    .cityName(provinceName)
                    .provinceName(cityName)
                    .appVersion(appVersion)
                    .type(NumberUtil.isPositiveInteger(spId) ? ShopTypeEnum.SP_TYPE.getKey() : ShopTypeEnum.CHANNEL_TYPE.getKey())
                    .appType(appType)
                    .accessType(accessType)
                    .build();
        } catch (Exception e) {
            log.error("[{}], 获取请求头信息异常: {}", TAG, e.getMessage(), e);
            throw new ParamsException("请求头信息参数异常e:{}", e.toString());
        }
    }

    /**
     * 用户设备信息
     *
     * @param request request
     * @return String
     * @author xujie
     * @since 2021/05/18
     */
    protected static String getDeviceInfo(HttpServletRequest request) {

        return request.getHeader("User-Agent");
    }

    protected UserAuthLoginDIO getUserAuthLogin(HttpServletRequest request, String code, Integer entrance) {

        // head参数
        HeaderBO header = getAllHeader(request);
        String uuid = UUID.randomUUID().toString();
        UserAuthLoginDIO dio = new UserAuthLoginDIO();
        dio.setUuid(uuid);
        dio.setCode(code);
        dio.setIp(HttpUtil.getIPAddress(request));
        dio.setDeviceInfo(getDeviceInfo(request));
        dio.setProvince(header.getProvinceName());
        dio.setCity(header.getCityName());
        dio.setChannelId(header.getChannelId());
        dio.setOrderChannelId(header.getOrderChannelId());
        dio.setPlat(header.getPlatId());
        dio.setSpId(header.getSpId());
        dio.setShopId(header.getShopId());
        dio.setAppType(header.getAppType());
        dio.setAppVersion(header.getAppVersion());
        dio.setAccessType(header.getAccessType());
        // TODO: 后面改为必填
        if(NumberUtil.isNotNullOrZero(entrance)){
            dio.setEntrance(entrance);
        }else{
            dio.setEntrance(0);
        }

        return dio;
    }

}
